package com.tomcat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class HttpRequest {
    private Socket socket;

    private String method;

    private String requestURI;

    public HttpRequest(Socket socket) {
        this.socket = socket;
        parseHttpRequestLine();
    }

    public String getRequestURI() {
        return requestURI;
    }

    public String getMethod() {
        return method;
    }

    // 解析Http请求行
    public void parseHttpRequestLine() {
        try {
            InputStream inputStream = socket.getInputStream();
            BufferedReader bufferedReader =
                    new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            // 获取请求行
            String requestLine = bufferedReader.readLine();
            String[] strings = requestLine.split(" ");
            method = strings[0];
            requestURI = strings[1];
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
